﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Cmn;
using SH.STD.Model;
using SH.STD.DataProvider;
using System.Data;

namespace SH.STD.Web.Project
{
    public partial class ProjectList : System.Web.UI.Page
    {
        public string URL;
        public string Category;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Category = Request.QueryString["Category"];
            if (this.Category.ToInt() == 0)
            {
                this.lblTitle.Text = "科研项目管理";
                this.Master.SetTab(TabType.Project0);
                
            }
            else
            {
                this.lblTitle.Text = "标准项目管理";
                this.Master.SetTab(TabType.Project1);
            }

            this.Page.Title = this.lblTitle.Text+"-科技处信息管理系统-公安部上海消防研究所";

            this.URL = "javascript:window.location.href='ProjectManage.aspx?Category=" + this.Category + "'";
            if (!Page.IsPostBack)
            {
                this.BindData(this.GetSql());
                this.BindParentType();
                this.BindDepartment();
                this.BindStatus();
            }
        }

        private void BindData(string sql)
        {
            var dt = Cmn.DB.getDataTable(sql);

            this.rptLastestProjects.DataSource = dt;
            this.rptLastestProjects.DataBind();
            this.lblCnt1.Text = dt.Rows.Count.ToString();
        }

        private void BindParentType()
        {
            var sql = "select ID,Name from ProjectType where ParentID is null and Status=1 order by ID";
            this.ddlParentProjectType.Items.Clear();
            this.ddlParentProjectType.Items.Add(new ListItem("-请选择-", "-1"));
            var dt = Cmn.DB.getDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlParentProjectType.Items.Add(new ListItem(dt.Rows[i][1].ToString(), dt.Rows[i][0].ToString()));
            }
        }

        private void BindDepartment()
        {
            var sql = "select ID,Name from Department where Status=1 order by ID";
            this.ddlDepartment.Items.Clear();
            this.ddlDepartment.Items.Add(new ListItem("-请选择-", "-1"));
            var dt = Cmn.DB.getDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlDepartment.Items.Add(new ListItem(dt.Rows[i][1].ToString(), dt.Rows[i][0].ToString()));
            }
        }

        private void BindStatus()
        {
            var sql = "select ID,Name from ProjectStatus where Status=1 order by ID";
            this.ddlProjectStatus.Items.Clear();
            this.ddlProjectStatus.Items.Add(new ListItem("-请选择-", "-1"));
            var dt = Cmn.DB.getDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlProjectStatus.Items.Add(new ListItem(dt.Rows[i][1].ToString(), dt.Rows[i][0].ToString()));
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindData(this.GetSql());
        }
        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.txtMemberName.Text = String.Empty;
            this.txtProjectName.Text = String.Empty;
            this.txtProjectID.Text = String.Empty;
            this.txtStartDate.Text = String.Empty;
            this.txtEndDate.Text = String.Empty;
            this.ddlDepartment.SelectedIndex = -1;
            this.ddlParentProjectType.SelectedIndex = -1;
            this.ddlProjectStatus.SelectedIndex = -1;

            this.BindData(this.GetSql());
        }

        private string GetSql()
        {
            var ret = @"
select a.ID,a.Name,a.Category,b.Name as ProjectStatus,a.StartDate,a.EndDate,c.Name as DepName
from Project a
left join ProjectStatus b
on a.StatusID=b.ID
left join Department c
on a.DepID=c.ID
where a.Status=1 and ";
            var where = String.Empty;
            if (this.ddlDepartment.SelectedIndex > 0)
            {
                where += "a.DepID=" + this.ddlDepartment.SelectedValue + " and ";
            }
            if (this.ddlProjectStatus.SelectedIndex > 0)
            {
                where += "a.StatusID=" + this.ddlProjectStatus.SelectedValue + " and ";
            }
            if (this.ddlParentProjectType.SelectedIndex > 0)
            {
                where += "a.ParentTypeID=" + this.ddlParentProjectType.SelectedValue + " and ";
            }
            if (!String.IsNullOrEmpty(this.txtStartDate.Text.Trim()))
            {
                where += "a.StartDate>='" + this.txtStartDate.Text.Trim() + "' and ";
            }
            if (!String.IsNullOrEmpty(this.txtEndDate.Text.Trim()))
            {
                where += "a.EndDate<='" + this.txtEndDate.Text.Trim() + "' and ";
            }
            if (!String.IsNullOrEmpty(this.txtMemberName.Text.Trim()))
            {
                where +=
@"a.ID in 
(   
    select a.ProjectID 
    from ProjectMember a 
    join Employee b
    on b.Name like '%" + this.txtMemberName.Text.Trim() + @"%'
) and ";
            }
            if (!String.IsNullOrEmpty(this.txtProjectName.Text.Trim()))
            {
                where += "a.Name like '%" + this.txtProjectName.Text.Trim() + "%' and ";
            }
            ret = where == String.Empty ? ret.Substring(0, ret.LastIndexOf("and ")) : ret + " " + where.Substring(0, where.LastIndexOf("and "));

            return ret;
        }

        public static string ExportTable(DataTable dt)
        {
            var columnNames = new Dictionary<string, string>();
            columnNames.Add("项目名称", "Name");
            columnNames.Add("项目状态", "ProjectStatus");
            columnNames.Add("开始日期", "StartDate");
            columnNames.Add("结束日期", "EndDate");
            columnNames.Add("所属部门", "DepName");
            string ret = "<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">";
            ret += "<tr style=\"font-weight: bold; white-space: nowrap;\">";
            foreach (var col in columnNames)
            {
                ret += "<td>" + col.Key + "</td>";
            }
            ret += "</tr>";
            foreach (DataRow row in dt.Rows)
            {
                ret += "<tr>";
                foreach (var col in columnNames)
                {
                    var colValue = (row[col.Value].ToString() != "undefined" ? row[col.Value].ToString() : "");
                    if (col.Value.Contains("Date") && !String.IsNullOrEmpty(colValue))
                    {
                        colValue = DateTime.Parse(colValue).ToString("yyyy-MM-dd");
                    }
                    ret += "<td>" + colValue + "</td>";
                }
                ret += "</tr>";
            }
            ret += "</table>";

            return ret;
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            var dt = Cmn.DB.getDataTable(this.GetSql());
            ExportDsToXls(this.Page, HttpUtility.UrlEncode(this.lblTitle.Text, System.Text.Encoding.UTF8), dt);
        }

        public static void ExportDsToXls(Page page, string fileName, DataTable dt)
        {
            page.Response.Clear();
            page.Response.Buffer = true;
            page.Response.Charset = "GB2312";
            page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls");
            page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            page.Response.ContentType = "application/ms-excel";
            page.EnableViewState = false;
            page.Response.Write(ExportTable(dt));
            page.Response.End();
        }
    }
}